package com.woniuxy.hotel.user.service.serviceimpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.poi.hpsf.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.hotel.user.mapper.CustomerMapper;
import com.woniuxy.hotel.user.mapper.VipMapper;
import com.woniuxy.hotel.user.pojo.Customer;
import com.woniuxy.hotel.user.pojo.Vip;
import com.woniuxy.hotel.user.service.CustomerService;
import com.woniuxy.hotel.user.util.Page;


@Service
public class CustomerServiceImpl implements CustomerService{
	
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private VipMapper vipMapper;
	
	@Override
	public Customer findCustomerByIdentity(String identity) {
		return customerMapper.findCustomerByIdentity(identity);
	}
	
	@Override
	public List<Customer> showAllCustomer() {
		List<Customer> customers=customerMapper.showAll();
		return customers;
	}

	@Override
	public Collection<Customer> showAllCustomerIsVip() {
		Collection<Customer> customers=customerMapper.showAllIsVip();
		return customers;
	}


	@Override
	public Customer findLoginInfo(String account) {
		return customerMapper.findLoginInfo(account);
	}

	@Override
	public Page<Customer> findCustomer(Customer customer, Page<Customer> page) {
		if(customer.getName()!=null&&customer.getName().length()!=0){
			customer.setName("%"+customer.getName()+"%");
		}
		if(customer.getVip()!=null){
			if(customer.getVip().getVipId()!=null&&customer.getVip().getVipId().length()!=0){
				Vip vip=customer.getVip();
				String vipId="%"+vip.getVipId()+"%";
				vip.setVipId(vipId);
				customer.setVip(vip);
			}
		}
		if(page.getCurrentPage()==0){
			page.setCurrentPage(1);
		}
		//每页查询10条数据
		if(page.getCount()==0){
			page.setCount(10);
		}
		page.setIndex((page.getCurrentPage()-1)*10);
		page.setData(customerMapper.findCustome(customer,page));
		if (page.getTotal() == 0) {
			page.setTotal(customerMapper.getTotal(customer));
			page.setTotalSize(page.getTotal()%10==0?page.getTotal()/10:page.getTotal()/10+1);
		}
		return page;
	}

	@Override
	public Customer findCustomerById(Integer id) {
		Customer customer=customerMapper.selectByPrimaryKey(id);
		if(customer==null){
			throw new RuntimeException("没有该宾客");
		}
		return customer;
	}

	@Override
	public List<String[]> updateBatchPM(List<Vip> vips) {
		List<String[]> results=new ArrayList<>();
		for(Vip vip:vips){
			String[] result=new String[3];
			result[0]=vip.getVipId();
			result[1]=vip.getPadmoney().toString();
			int row=vipMapper.updateBatchPM(vip);
			if(row!=1){
				result[2]="更新失败，会员编号不存在";
			}else{
				result[2]="更新成功";
			}
			results.add(result);
		}
		return results;
	}

}
